﻿/*
Create by ai829(www.80s.net.cn)
Create for 2009 RIAMeeting
*/
package
{
import flash.display.*;
import flash.events.*;
import flash.text.*;

// Import Papervision3D
import org.papervision3d.view.*;
import org.papervision3d.events.FileLoadEvent;
import org.papervision3d.scenes.*;
import org.papervision3d.cameras.Camera3D;
import org.papervision3d.objects.parsers.*;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.materials.*;
import org.papervision3d.render.*;


public class Main extends Sprite
{
	// ___________________________________________________________________ vars3D

	public var viewport  :Viewport3D;
	public var scene     :Scene3D;
	public var camera    :Camera3D;
	public var sphere    :*;
	public var plane     :Plane;
	public var renderer  :BasicRenderEngine;
	public var loading_info:TextField;

	// ___________________________________________________________________ main

	function Main()
	{
		init3D();
		// onEnterFrame
		this.addEventListener( Event.ENTER_FRAME, loop3D );
	}


	// ___________________________________________________________________ init3D

	function init3D():void
	{
		viewport = new Viewport3D(550, 400, false,false);
		addChild( viewport );
		
		renderer = new BasicRenderEngine();
		
		// Create scene
		scene = new Scene3D();

		// Create camera
		camera = new Camera3D();
		
		loading_info = new TextField();
		loading_info.autoSize = TextFieldAutoSize.LEFT;
		loading_info.selectable = false;

		// Add Model sphere
		addModel();
		
		// add loading text		
		addChild( loading_info );
	}


	// ___________________________________________________________________ loop

	function addModel():void
	{
		loading_info.text = "loading md2 material from a external file";
		// Create md2 material from a external file
		var md2material :BitmapFileMaterial = new BitmapFileMaterial();
		md2material.addEventListener(FileLoadEvent.LOAD_COMPLETE, handleFileLoaded);
		md2material.texture = "http://blog.80s.net.cn/wp-content/uploads/2009/04/blue.jpg";
		
	}
	
	function handleFileLoaded(e:*):void {
		sphere = new MD2();
		//any md2 file you likes
		sphere.load("http://blog.80s.net.cn/wp-content/uploads/2009/04/monster.md2", e.currentTarget, 8);
		sphere.rotationX = -90;
		sphere.scale = 10;
		sphere.addEventListener(FileLoadEvent.LOAD_COMPLETE, loadCompleteHandler);
		sphere.addEventListener(ProgressEvent.PROGRESS, loadProgressHandler);
		scene.addChild( sphere );
	}
	
	function loadCompleteHandler( event:FileLoadEvent ):void
	{
		loading_info.text = "finished!";
	}
	
	function loadProgressHandler( event:ProgressEvent ):void
	{
		loading_info.text = "loading md2 file "+ Math.floor(event.bytesLoaded/event.bytesTotal*100) +"%";
	}

	// ___________________________________________________________________ loop

	function loop3D(event:Event):void
	{
		var tX:Number= mouseX/2;
		var tY:Number= mouseY/2;
			
		var dx:Number= tX-camera.x;
		var dy:Number= tY-camera.y;
			
		camera.x += dx * .3 ;	
		camera.y += dy * .3 ;
		
		sphere.roll(1);
		// Render the scene
		renderer.renderScene(scene, camera, viewport);
	}
}
}